from typing import Dict, Any
import jwt

from settings import JwtConfig

class JWTUtil:
    
    @staticmethod
    def encode_dict(encode_data: Dict[str, Any]) -> str:
        """根据字典信息生成 token

        Args:
            encode_data (Dict[str, Any]): 负载信息

        Returns:
            str: token
        """
        return jwt.encode(
            encode_data, 
            JwtConfig.SECRET_KEY, 
            algorithm=JwtConfig.JWT_ALGORITHM
        )
       
    @staticmethod 
    def decode_dict(token: str) -> Dict[str, Any]:
        """根据 token 解析出负载信息

        Args:
            token (str): token 信息

        Returns:
            Dict[str, Any]: 负载信息
        """
        return jwt.decode(
            token, 
            JwtConfig.SECRET_KEY, 
            algorithms=[JwtConfig.JWT_ALGORITHM]
        )